<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增文档')" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-document-add" enctype="multipart/form-data">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">文档分类：</label>
                <div class="col-sm-8">
                    <select name="categoryId" class="form-control select2-hidden-accessible" id="categoryId" required>
                        <option value="">请选择分类</option>
                        <option th:each="category : ${categories}" th:value="${category.categoryId}" th:text="${category.categoryName}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">文档标题：</label>
                <div class="col-sm-8">
                    <input name="title" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">上传文档：</label>
                <div class="col-sm-8">
                    <input name="file" type="file" required accept=".txt,.doc,.docx">
                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 支持 .txt、.doc、.docx 格式，最大50MB</span>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-8 col-sm-offset-3">
                    <button type="submit" class="btn btn-primary">提交</button>
                    <button type="reset" class="btn btn-default m-l-5">重置</button>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <script type="text/javascript">
        var prefix = ctx + "system/document";
        
        $(function() {
            // 初始化Select2
            $(".select2-hidden-accessible").select2({
                width: "100%"
            });
            
            $("#form-document-add").validate({
                focusCleanup: true,
                submitHandler: function(form) {
                    var formData = new FormData(form);
                    
                    $.ajax({
                        type: "post",
                        url: prefix + "/add",
                        data: formData,
                        contentType: false,
                        processData: false,
                        success: function(result) {
                            $.operate.successCallback(result);
                        },
                        error: function(xhr, status, error) {
                            $.modal.alertError("上传失败: " + error);
                        }
                    });
                }
            });
        });
    </script>
</body>
</html>